package org.lgy.hadoop.tempreature;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

	private static final int MISSING = 9999;

	@Override
	public void map(LongWritable key, Text value, Context context) 
		throws IOException, InterruptedException {

		String line = value.toString();
		String year = line.substring(10, 14);
		int airTemperature;
		if (line.charAr(24) == '+') {
			airTemperature = Integer.parseInt(line.substring(25, 29));
		} else {
			airTemperature = Integer.parseInt(line.substring(24, 29));
		}
		String quality = line.substring(29, line.length);
		if (airTemperature != MISSING && quality.matchs("[01459]")) {
			context.write(new Text(year), new IntWritable(airTemperature));
		}
	}
}
